home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 43 / Amiga Format CD43 (1999)(Future Publishing)(GB)(Track 1 of 2)[!][issue 1999-09].iso / -serious- / programming / other / python-1.52 / lib / python1.5 / test / test_zlib.py < prev    next >
Text File  |  1999-06-14  |  4KB  |  162 lines

  1. import zlib
  2. import sys
  3. import imp
  4. import string
  5.  
  6. try:
  7.     t = imp.find_module('test_zlib')
  8.     file = t[0]
  9. except ImportError:
  10.     file = open(__file__)
  11. buf = file.read() * 8
  12. file.close()
  13.  
  14. # test the chucksums (hex so the test doesn't break on 64-bit machines)
  15. print hex(zlib.crc32('penguin')), hex(zlib.crc32('penguin', 1))
  16. print hex(zlib.adler32('penguin')), hex(zlib.adler32('penguin', 1))
  17.  
  18. # make sure we generate some expected errors
  19. try:
  20.     zlib.compress('ERROR', zlib.MAX_WBITS + 1)
  21. except zlib.error, msg:
  22.     print "expecting", msg
  23. try:
  24.     zlib.compressobj(1, 8, 0)
  25. except ValueError, msg:
  26.     print "expecting", msg
  27. try:
  28.     zlib.decompressobj(0)
  29. except ValueError, msg:
  30.     print "expecting", msg
  31.  
  32. x = zlib.compress(buf)
  33. y = zlib.decompress(x)
  34. if buf != y:
  35.     print "normal compression/decompression failed"
  36. else:
  37.     print "normal compression/decompression succeeded"
  38.  
  39. buf = buf * 16
  40.  
  41. co = zlib.compressobj(8, 8, -15)
  42. x1 = co.compress(buf)
  43. x2 = co.flush()
  44. x = x1 + x2
  45.  
  46. dc = zlib.decompressobj(-15)
  47. y1 = dc.decompress(x)
  48. y2 = dc.flush()
  49. y = y1 + y2
  50. if buf != y:
  51.     print "compress/decompression obj failed"
  52. else:
  53.     print "compress/decompression obj succeeded"
  54.  
  55. co = zlib.compressobj(2, 8, -12, 9, 1)
  56. bufs = []
  57. for i in range(0, len(buf), 256):
  58.     bufs.append(co.compress(buf[i:i+256]))
  59. bufs.append(co.flush())
  60. combuf = string.join(bufs, '')
  61.  
  62. decomp1 = zlib.decompress(combuf, -12, -5)
  63. if decomp1 != buf:
  64.     print "decompress with init options failed"
  65. else:
  66.     print "decompress with init options succeeded"
  67.  
  68. deco = zlib.decompressobj(-12)
  69. bufs = []
  70. for i in range(0, len(combuf), 128):
  71.     bufs.append(deco.decompress(combuf[i:i+128]))
  72. bufs.append(deco.flush())
  73. decomp2 = string.join(buf, '')
  74. if decomp2 != buf:
  75.     print "decompressobj with init options failed"
  76. else:
  77.     print "decompressobj with init options succeeded"
  78.  
  79. # Test flush() with the various options, using all the different levels
  80. # in order to provide more variations.
  81. for sync in [zlib.Z_NO_FLUSH, zlib.Z_SYNC_FLUSH, zlib.Z_FULL_FLUSH]:
  82.     for level in range(10):
  83.     obj = zlib.compressobj( level )
  84.         d = obj.compress( buf[:3000] )
  85.     d = d + obj.flush( sync )
  86.     d = d + obj.compress( buf[3000:] )
  87.     d = d + obj.flush()
  88.     if zlib.decompress(d) != buf:
  89.         print "Decompress failed: flush mode=%i, level=%i" % (sync,level)
  90.     del obj
  91.  
  92. def ignore():
  93.     """An empty function with a big string.
  94.  
  95.     Make the compression algorithm work a little harder.
  96.     """
  97.  
  98.     """
  99. LAERTES 
  100.  
  101.        O, fear me not.
  102.        I stay too long: but here my father comes.
  103.  
  104.        Enter POLONIUS
  105.  
  106.        A double blessing is a double grace,
  107.        Occasion smiles upon a second leave.
  108.  
  109. LORD POLONIUS 
  110.  
  111.        Yet here, Laertes! aboard, aboard, for shame!
  112.        The wind sits in the shoulder of your sail,
  113.        And you are stay'd for. There; my blessing with thee!
  114.        And these few precepts in thy memory
  115.        See thou character. Give thy thoughts no tongue,
  116.        Nor any unproportioned thought his act.
  117.        Be thou familiar, but by no means vulgar.
  118.        Those friends thou hast, and their adoption tried,
  119.        Grapple them to thy soul with hoops of steel;
  120.        But do not dull thy palm with entertainment
  121.        Of each new-hatch'd, unfledged comrade. Beware
  122.        Of entrance to a quarrel, but being in,
  123.        Bear't that the opposed may beware of thee.
  124.        Give every man thy ear, but few thy voice;
  125.        Take each man's censure, but reserve thy judgment.
  126.        Costly thy habit as thy purse can buy,
  127.        But not express'd in fancy; rich, not gaudy;
  128.        For the apparel oft proclaims the man,
  129.        And they in France of the best rank and station
  130.        Are of a most select and generous chief in that.
  131.        Neither a borrower nor a lender be;
  132.        For loan oft loses both itself and friend,
  133.        And borrowing dulls the edge of husbandry.
  134.        This above all: to thine ownself be true,
  135.        And it must follow, as the night the day,
  136.        Thou canst not then be false to any man.
  137.        Farewell: my blessing season this in thee!
  138.  
  139. LAERTES 
  140.  
  141.        Most humbly do I take my leave, my lord.
  142.  
  143. LORD POLONIUS 
  144.  
  145.        The time invites you; go; your servants tend.
  146.  
  147. LAERTES 
  148.  
  149.        Farewell, Ophelia; and remember well
  150.        What I have said to you.
  151.  
  152. OPHELIA 
  153.  
  154.        'Tis in my memory lock'd,
  155.        And you yourself shall keep the key of it.
  156.  
  157. LAERTES 
  158.  
  159.        Farewell.
  160. """
  161.  
  162.